package RT::Group;

use strict;
no warnings qw(redefine);

$RT::Group::RIGHTS->{'AdminOnCall'} = 'Modify group OnCall Data',  # loc_pair
$RT::ACE::LOWERCASERIGHTNAMES{ 'adminoncall' } = "AdminOnCall";

sub PrimaryOnCallUsers {

    my $self = shift;
    return $self->_OnCallUsers("PrimaryOnCall", @_);

}

sub SecondaryOnCallUsers {

    my $self = shift;
    return $self->_OnCallUsers("SecondaryOnCall", @_);

}

sub TeamLeadUsers {

    my $self = shift;
    return $self->_OnCallUsers("TeamLead", @_);

}

sub _OnCallUsers {

    my $self = shift;
    my $type = shift;

    my $attr = $self->FirstAttribute($type);
    my @uids = ref($attr) ? split(',', $attr->Content) : $attr;
    my @users;

    foreach (@uids) {
        my $user = RT::User->new($self->CurrentUser);
        my ($rv, $msg) = $user->Load($_);
        push (@users, $user) if $rv;
    }

    return @users;

}

sub SetPrimaryOnCall {

    my $self = shift;
    return $self->_SetOnCall("PrimaryOnCall", @_);

}

sub SetSecondaryOnCall {

    my $self = shift;
    return $self->_SetOnCall("SecondaryOnCall", @_);

}

sub SetTeamLead {

    my $self = shift;
    return $self->_SetOnCall("TeamLead", @_);

}

sub _SetOnCall {

    my $self = shift;
    my $type = shift;

    return $self->SetAttribute(Name => $type, Content => join(',', @_));

}

sub IsPrimaryOnCall {

    my $self = shift;
    return $self->_IsOnCall("PrimaryOnCall", @_);

}

sub IsSecondaryOnCall {

    my $self = shift;
    return $self->_IsOnCall("SecondaryOnCall", @_);

}

sub IsTeamLead {

    my $self = shift;
    return $self->_IsOnCall("TeamLead", @_);

}

sub _IsOnCall {

    my $self = shift;
    my $type = shift;
    my $user = shift;

    foreach ( $self->_OnCallUsers($type) ) {
        return 1 if ($user->Id == $_->Id);
    }
    
    return 0;

}

1;
